#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

#include "doh_client.hpp"
#include "tools.hpp"

// 使用示例
int main(int argc, char *argv[]) {
    try {
        // 初始化libcurl(全局初始化)
        curl_global_init(CURL_GLOBAL_DEFAULT);

        // 创建DoH客户端实例
        DoHClient client;

        // 查询域名
        std::string domain = "ap4-tls.agora.io";
        if (argc > 1) {
            // 检查是否是帮助请求
            std::string arg1 = argv[1];
            if (arg1 == "-h" || arg1 == "--help") {
                print_usage(argv[0]);
                return 0;
            }
            domain = argv[1];
        } else {
            domain = "ap4-tls.agora.io";  // 默认域名
            std::cout << "No domain specified. Using default: " << domain << std::endl;
        }
        std::cout << "Querying " << domain << "..." << std::endl;

        // 设置DoH服务器
        if (argc > 2) {
            std::string server = argv[2];
            client = DoHClient(server);
        } else {
            client = DoHClient();
        }

        // 确定查询方法
        DoHMethod method = DoHMethod::JSON_GET;  // 默认使用JSON GET
        if (argc > 3) {
            std::string methodStr = argv[3];
            std::transform(methodStr.begin(), methodStr.end(), methodStr.begin(),
                           [](unsigned char c) { return std::tolower(c); });

            if (methodStr == "get") {
                method = DoHMethod::GET;
            } else if (methodStr == "post") {
                method = DoHMethod::POST;
            } else if (methodStr == "json" || methodStr == "json_get") {
                method = DoHMethod::JSON_GET;
            } else {
                std::cerr << "Unknown method: " << methodStr << ". Using default JSON_GET." << std::endl;
            }
        }

        // 执行A记录查询
        auto records = client.query(domain, DNSRecordType::A, method);

        // 输出结果
        if (records.empty()) {
            std::cout << "No records found." << std::endl;
        } else {
            std::cout << "DNS records for " << domain << ":" << std::endl;
            for (const auto &record : records) {
                std::cout << "Name: " << record.name << std::endl;
                std::cout << "Type: ";

                switch (record.type) {
                    case DNSRecordType::A:
                        std::cout << "A";
                        break;
                    case DNSRecordType::AAAA:
                        std::cout << "AAAA";
                        break;
                    case DNSRecordType::CNAME:
                        std::cout << "CNAME";
                        break;
                    case DNSRecordType::MX:
                        std::cout << "MX";
                        break;
                    case DNSRecordType::NS:
                        std::cout << "NS";
                        break;
                    case DNSRecordType::TXT:
                        std::cout << "TXT";
                        break;
                    default:
                        std::cout << static_cast<int>(record.type);
                        break;
                }

                std::cout << std::endl;
                std::cout << "TTL: " << record.ttl << " seconds" << std::endl;
                std::cout << "Data: " << record.data << std::endl;
                std::cout << "------------------------" << std::endl;
            }
        }

        // 清理libcurl资源
        curl_global_cleanup();

        return 0;
    } catch (const std::exception &e) {
        std::cerr << "Exception: " << e.what() << std::endl;
        return 1;
    }
}